<?php
class FilterForm extends Form{
	private $filter_group=false, $filter=false;
	
	function FilterForm()
	{
		Form::Form('FilterForm');
		
		CGlobal::$website_title='Thuộc tính chuyên mục';
		
		$fgroup_id=(int)Url::get('fgroup_id',0);
		
		if($fgroup_id)
		{
			$this->filter_group	=	DB::select('filter_group','id='.$fgroup_id);
		}
		
		if(!$this->filter_group){
			Url::redirect_current();
		}
		
		
		#############################################################################
		#Remove filter_group khỏi danh mục
		$catid_remove = (int)Url::get('catid_remove',0);
		
		if($catid_remove)
		{
			Category::remove_cat_group_filer($catid_remove,$fgroup_id);
			
			Url::redirect_current(array('cmd','fgroup_id'));
		}
		
		#Remove filter_group khỏi danh mục
		#############################################################################
		
		#############################################################################
		#Xóa filter
		$f_del=(int)Url::get('f_del',0);
		
		if($f_del)
		{
			$this->filter	= DB::select('filter','id='.$f_del);
			
			if($this->filter && $this->filter['fgid'] == $this->filter_group['id'])
			{
				DB::delete("filter",		"id=$f_del");
			}
			
			AZLib::getFilters(1);
			
			Url::redirect_current(array('cmd','fgroup_id'));
		}
		#Xóa filter
		#############################################################################
	}
	
	function on_submit()
	{
		$gfsubmit = Url::get("gfsubmit");
		
		if($gfsubmit)//Cập nhật thuộc tính
		{
			$fgid = (int)implode(array_keys($gfsubmit));
			
			if($fgid == $this->filter_group['id'])
			{
				$gf_name = Url::get('gf_name');
				
				if($gf_name !='' && $this->filter_group['name'] != $gf_name)//Đổi tên
				{
					DB::update('filter_group', array("name" => $gf_name) , "id=$fgid");
					
					AZLib::getFilters(1);
				}
			}
			
			Url::redirect_current(array('cmd','fgroup_id'));
		}
		
		$fsubmit = Url::get("fsubmit");
		
		if($fsubmit)//Cập nhật thuộc tính
		{
			$filter_id = (int)implode(array_keys($fsubmit));
			
			if($filter_id)
			{
				$filter = DB::select("filter","id=$filter_id");
				
				if($filter)
				{
					$f_name = Url::get('f_name');
					$f_pos 	= Url::get('f_pos');
					
					if(isset($f_name[$filter_id],$f_pos[$filter_id]) && $f_name[$filter_id] && $f_pos[$filter_id])
					{
						$f_name 	= $f_name[$filter_id];
						$f_pos 	= $f_pos[$filter_id];
					
						if($filter['name'] != $f_name || $filter['pos'] != $f_pos)//Đổi tên, vị trí
						{
							$max_pos	=	(int)DB::fetch("SELECT max(pos) AS max_pos FROM filter WHERE fgid={$filter['fgid']}",'max_pos',0);
				
							if($f_pos > $max_pos)
							{
								$f_pos = $max_pos;
							}
							elseif($f_pos<=0)
							{
								$f_pos = $filter['pos'];
							}
						
							DB::update('filter', array("name" => $f_name, "pos" => $f_pos) , "id=$filter_id");
					
							if(DB::select('filter',"pos=$f_pos AND id!=$filter_id AND fgid={$filter['fgid']}"))
							{
								DB::query("UPDATE filter SET pos=pos+1 WHERE pos>=$f_pos AND pos<={$filter['pos']} AND fgid={$filter['fgid']} AND id!=$filter_id");
							}
							
							AZLib::getFilters(1);
						}
					}
				}
			}
			
			Url::redirect_current(array('cmd','fgroup_id'));
		}
		
		$submit = Url::get("submit");
		
		if($submit == "Thêm thuộc tính")
		{
			$filter_name	= Url::get('filter_name');
			$filter_pos		= Url::get('filter_pos');
			
			if($filter_name!='' && $filter_pos)
			{
				$filter =	array(	
											'name'		=>$filter_name,
											'fgid'		=>$this->filter_group['id'],
										 ); 

				$max_pos	=	(int)DB::fetch("SELECT max(pos) AS max_pos FROM filter WHERE fgid=".$this->filter_group['id'],'max_pos',0);
			
				if($filter_pos	>	$max_pos || $filter_pos <= 0)
				{
					$filter_pos	=	$max_pos+1;
				}
				
				$filter['pos'] = $filter_pos;
				
				$id = DB::insert('filter', $filter);
				
				if($id && DB::select('filter','pos="'.$filter_pos.'" AND id!="'.$id.'" AND fgid="'.$this->filter_group['id'].'"')){
					DB::query('UPDATE filter SET pos=pos+1 WHERE pos>='.$filter_pos.' AND fgid="'.$this->filter_group['id'].'" AND id!='.$id);
				}
			}
			
			AZLib::getFilters(1);
		}
		
		Url::redirect_current(array('cmd','fgroup_id'));
	}
	
	function draw()
	{	
		global $display;
		$this->beginForm(true);
		
		$filters = array();
		
		$max_pos_filter = 1;
		
		$max_pos_filter = 1 + DB::fetch("SELECT MAX(pos) AS max_pos FROM filter WHERE fgid=".$this->filter_group['id'],"max_pos",0);
	
		$re = DB::query("SELECT * FROM filter WHERE fgid=".$this->filter_group['id']." ORDER BY pos");
		
		if($re)
		{
			while($filter = mysql_fetch_assoc($re))
			{
				$filter['del'] 	= Url::build_current(array('cmd','fgroup_id','f_del'=>$filter['id']));
				
				$filters[$filter['id']] = $filter;
			}
		}
		
		$display->add('filters',$filters);
		
		$display->add('group_filter_id',	$this->filter_group['id']);
		$display->add('group_filter_name',	$this->filter_group['name']);
		
		$cat_arr = array();
		
		if($this->filter_group['catids'])
		{
			$catid_arr = explode(',',$this->filter_group['catids']);
			
			foreach ($catid_arr as $catid)
			{
				$cat =  array(
							'id' 	=> $catid,
							'name' 	=> CGlobal::$allCategories[$catid]['name'],
							);
							
				if(isset(CGlobal::$allCategories[$catid]))
				{
					$cat['name'] = CGlobal::$allCategories[$catid]['name'];
				}
				else{
					$cat['name'] = "<font color=red>(Đã xóa)</font>";
				}
				
				$cat_arr[$catid] = $cat;
			}
		}
		
		//$display->add('group_catids',		$this->filter_group['catids']);
		
		$display->add('cat_arr',			$cat_arr);
		
		$display->add('group_pos',			$this->filter_group['pos']);

		$display->add('option_filter_pos',AZLib::getOptionNum(1,$max_pos_filter,$max_pos_filter));
		
		$display->output('Filter');
		$this->endForm();
	}
}
?>